<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xx
 * Date: 14-1-23
 * Time: 下午4:49
 * To change this template use File | Settings | File Templates.
 */

/**
 * Class Login_model
 * 登录模型
 */
class Login_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();

        /**
         * 对模块的返回值进行扩展
         * 1000-2000 登陆模块使用
         */
        $this->return_status[1001] = "密码不正确";
        $this->return_status[1002] = "用户不存在";

    }

    /**
     * 登录鉴权
     * @param $username
     * @param $pwd
     * @param $role
     */
    public function do_login($username, $pwd)
    {
        $ret = new Ret($this);

        $user = $this->get_user_by_username($username);
        if ($user) {
            if ($user['user_pwd'] === $pwd) {
                $ret->set_code(0);
                unset($user['user_pwd']); //清除密码数据
                $ret->set_data($user);
            } else {
                $ret->set_code(1001); //密码不正确
            }
        } else {
            $ret->set_code(1002); //用户不存在
        }

        return $ret;
    }

    /**
     * 通过用户名获取用户信息
     * @param $username
     * @return bool
     */
    private function get_user_by_username($username)
    {
        $row = FALSE;
        $sql = "SELECT * FROM " . XS_USER . " WHERE user_name = ? AND deleteflag = 0";
        $query = $this->db->query($sql, array($username));
        if ($query->num_rows() > 0) {
            $row = $query->row_array();
        }

        return $row;
    }
}